export function driveUeWebsocket(){
    let bStart = false
    var ws;
    let messageEventListenrs = [] //接收消息的监听器
    let timer_count = 0 //接收消息的监听器
    let interval_timer ;
    let serverUrl ;
    let _this = this;
    this.StartStreamer = function (serverurl) {
        //ws:  or  wss:
        serverUrl = serverurl
        useWebSocket(serverurl)
        bStart = true
      }
      function useWebSocket  (url) {
        if (!ws) {
          //TODO ws 不存在
          ws = new WebSocket(url) //布置到服务器之后，其他ip访问
    
          ws.binaryType = 'arraybuffer' //传输的是 ArrayBuffer 类型的数据
    
          websocketInit()
        } else {
          if (!isOnlineCurrUser()) {
            ws = null
            useWebSocket()
          }
        } // 开启定时器
    
        init_start_timer()
      }
      this.addMessageEventListener = function (listener) {
        messageEventListenrs.push(listener)
      }
    
      this.removeMessageEventListener = function (listener) {
        messageEventListenrs.remove(listener)
      }
      /**
       * websocket 的初始化
       */
    
      function websocketInit() {
        ws.onopen = function () {
          console.log('握手成功')
    
          if (ws.readyState == 1) {
            //ws进入连接状态，则每隔500毫秒发送一包数据
            //发起端，则先发起一则创建房间信息，及个人信息
            sendBaseEvent( {"type":1,"data":{"clientType":1}})
         
          }
        }
    
        ws.onmessage = function (e) {
          // console.log("带看onmessage: "+e.data);
          var msg = JSON.parse(e.data) //    console.log("messageobj",msg)
    
          if (msg.event != undefined) {
            console.log("event happend ,event:"+msg.event);
            if (messageEventListenrs.length > 0) {
                messageEventListenrs.forEach((listener) => {
                  listener.traiggerMessageEvent(msg)
                })
              }
          }
        }
    
        ws.onclose = function (e) {
          console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean) //后边要加断线重连
    
          if (bStart) {
            //开始状态下需要重连
            reconect()
          }
        }
    
        ws.onerror = function (err) {
          console.info('websocket 错误: ' + err)
        }
      }
      /**
       * 设置一个 30秒的轮询监听方法，避免页面关闭
       */
    
      function init_start_timer() {
        //重置计数器
        timer_count = 0
    
        if (interval_timer != null) {
          clearInterval(interval_timer)
          interval_timer = null
        }
    
        interval_timer = setInterval(function () {
          myTimer()
        }, 30000)
      }
      /**
       *定时器具体实现方法
       */
    
      function myTimer() {
        //TODO 如果超过半小时没有交互，则关闭计时器
        if (timer_count >= 30) {
          // clearInterval(interval_timer);
          sendHeartBit() //发送心跳包
        } else {
          timer_count += 1
        }
      }
      /**
       * 判断当前用户是否 还在线
       */
    
      function isOnlineCurrUser() {
        if (ws) {
          if (ws.readyState == WebSocket.OPEN) {
            return true
          } else {
            return false
          }
        } else {
          return false
        }
      }
    
      function reconect() {
        if (ws.readyState === WebSocket.OPEN) {
          ws.close()
        }
    
        ws = null
    
        _this.StartStreamer(serverUrl)
      } 
      this.sendEventFromOut = function ( data) {
        sendBaseEvent( data)
      }
      /////////////发送消息
      function sendBaseEvent( data) {
        timer_count = 0
    
        if (isOnlineCurrUser()) {
          
    
          ws.send(JSON.stringify(data))
        }
      }
    
      function sendHeartBit() {
        //发送心跳包
        timer_count = 0
    
        if (isOnlineCurrUser()) {
         
          ws.send('heartbeat')
        }
      }
}
